﻿using AntdUI;
using ImportPlcPointPosition.Models;
using ImportPlcPointPosition.Models.Table;
using ImportPlcPointPosition.Models.ViewModels;
using ImportPlcPointPosition.Utils;
using Microsoft.IdentityModel.Tokens;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImportPlcPointPosition.Forms {
    public partial class VirtualTableForm : BaseForm {
        public CksVirtualTable Model = null;
        public VirtualTableForm(CksVirtualTable context = null) {
            InitializeComponent();
            if (context != null) {
                Model = context;
                this.pageHeader1.Text = "编辑虚拟表";
                txtNotes.Text = $"{Model.Notes}";
                txtName.Text = $"{Model.TableName}";
                txtSeq.Text = $"{Model.Sequence}";
            } else {
                this.pageHeader1.Text = "新建虚拟表";
            }
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            DialogResult = DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e) {

            try {
                var item = Model ?? new CksVirtualTable();
                item.Notes = txtNotes.Text;
                item.TableName = txtName.Text;
                item.Sequence = txtSeq.Text.IsNullOrEmpty() ? 999 : txtSeq.Text.ObjToInt();

                if (item.TableName.IsNullOrEmpty()) {
                    AntdUI.Message.error(App.MainLayout, "虚拟表名称必填！", autoClose: 2);
                    return;
                }

                if (item.Id == 0)
                    SqlSugarHelper.MesDB.Insertable(item).ExecuteCommand();
                else
                    SqlSugarHelper.MesDB.Updateable(item).ExecuteCommand();

                DialogResult = DialogResult.OK;

            } catch (Exception ex) {
               
                AntdUI.Message.error(App.MainLayout, ex.Message);
            }

        }
    }
}
